/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import utils.DatabaseConnection;
import utils.ResultSetWrap;

/**
 *
 * @author KAV
 */
public class QueryInitializerBean implements SessionBean {

    private SessionContext context;

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
    }

    // </editor-fold>;
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate() {
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
    }
    private PreparedStatement ps = null;
    private Connection conn = null;

    public void init(String queryPattern, String jdbcConnection) throws NamingException, SQLException {
        Context c = new InitialContext();
        conn = DatabaseConnection.getInstance(jdbcConnection).getDataSource().getConnection();
        ps = conn.prepareStatement(queryPattern);
    }

    public ResultSetWrap resultQuery() throws SQLException {
        // statement.executeQuery, разбор ResultSet, закрытие соединения, возврат результата
        ResultSetWrap result = new ResultSetWrap(ps.executeQuery());
        closeAndClearPreparedStatement();
        return result;
    }

    public int resultUpdate() throws SQLException {
        int result = ps.executeUpdate();
        closeAndClearPreparedStatement();
        return result;
    }

    public void closeAndClearPreparedStatement() throws SQLException {
        conn.close();
        ps.close();
        ps = null;
        conn = null;
    }

    public void setParam(String type, String value, int i) throws SQLException {
        if (type.equals("Integer")) {
            int check = Integer.parseInt(value);
            if (check != 0) {
                ps.setInt(i, check);
            } else {
                ps.setNull(i, i);
            }
        } else if (type.equals("Double")) {
            ps.setDouble(i, Double.parseDouble(value));
        } else if (type.equals("String")) {
            ps.setString(i, value);
        }
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
}
